<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>layui</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link href="__LAYUI__/layui/css/layui.css" media="all" rel="stylesheet">
		<link href="__LAYUI__/adminui/dist/css/admin.css" media="all" rel="stylesheet">
	</head>
	<style>
		.layui-icon-ok{color:#c2c2c2!important;}
		.layui-form-checked:hover>div,
		.layui-form-checked>div {
			background-color: #16baaa !important;
		}

		.layui-form-checked:hover>i,
		.layui-form-checked>i {
			border-color: #16baaa !important;
		}

		.layui-panel {
			padding: 10px;
		}
	</style>
	{include file="info" /}
	<body>
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						<div class="layui-card-body">
							<div class="layui-tab">
								<div class="layui-tab-content">
									<div class="layui-tab-item layui-show">
										<div class="layui-form layui-form-pane">
											<div class="layui-form-item">
												<label class="layui-form-label">名称<em>*</em></label>
												<div class="layui-input-block">
													<input type="text" name="name" value="{$info.name?:''}"
														placeholder="请输入名称" class="layui-input">
												</div>
											</div>
											<div class="layui-form-item layui-form-text">
												<label class="layui-form-label">权限<em>*</em></label>
												<div class="layui-input-block">
													<div class="layui-panel" id="TreeAuth">
													</div>
												</div>
											</div>
											<div class="layui-form-item layui-form-text">
												<label class="layui-form-label">备注</label>
												<div class="layui-input-block">
													<textarea name="postscript" placeholder="请输入备注"
														class="layui-textarea">{$info.postscript?:''}</textarea>
												</div>
											</div>
											<div class="layui-form-item">
												<label class="layui-form-label">创建时间</label>
												<div class="layui-input-inline">
													{notempty name="$info"}
													<input type="text" class="layui-input" disabled
														value="{:date('Y-m-d H:i:s',$info.create_time)}">
													{else/}
													<input type="text" class="layui-input" disabled
														value="{:date('Y-m-d H:i:s',time())}">
													{/notempty}
												</div>
											</div>
											<div class="layui-form-item">
												<label class="layui-form-label">状态</label>
												<div class="layui-input-inline">
													{notempty name="$info"}
													<input type="checkbox" name="status" value="{$info.status?:1}"
														lay-skin="tag" lay-filter="tagstatus"
														title="{$info.status==1?'启用':'禁用'}" {if
														$info.status==1}checked{/if}>
													{else/}
													<input type="checkbox" name="status" value="1" lay-skin="tag"
														lay-filter="tagstatus" title="启用" checked>
													{/notempty}
												</div>
											</div>
											{notempty name="$info"}
											<input type="hidden" name="id" value="{$info.id}">
											{/notempty}
											<div
												class="layui-form-item {if MyAuto('myadmin','cate','edit')==false}layui-hide{/if}">
												<button class="layui-btn" lay-submit lay-filter="submit"> 保存 </button>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
<script>
	layui.use(["layer", "jquery", "form", "tree"], function() {
		var layer = layui.layer;
		var $ = layui.jquery;
		var form = layui.form;
		var tree = layui.tree;
		form.on("checkbox(tagstatus)", function(data) {
			if (data.elem.checked) {
				$("input[name='status']").val(1).attr("title", "启用").attr("checked", true);
				$(".layui-form-checkbox div").text("启用");
			} else {
				$("input[name='status']").val(2).attr("title", "禁用").removeAttr("checked");
				$(".layui-form-checkbox div").text("禁用");
			}
		});
		$(window).on("load", function() {
			var perlist = [{$info.permissions}];
			$.ajax({
				url: "{:url('menu/index')}",
				type: "post",
				success: function(res) {
					tree.render({
						elem: "#TreeAuth",
						data: res.data,
						showCheckbox: true, //是否显示复选框
						accordion: true, //是否手风琴模式
						id: "Treeid",
						oncheck: function(obj) {
							var childs = $(obj.elem).find("." + "layui-tree-pack")
								.find("input[same='layuiTreeCheck']");
							childs.each(function() {
								this.checked = false; //父级不选中子级
							});
							form.render("checkbox");
						}
					});
					//tree.setChecked("Treeid", [1,10,23]); 	//勾选指定节点
					tree.setChecked("Treeid", perlist); //勾选指定节点
				}
			});
			//获取所有选中的节点id
			function getCheckedId(data) {
				var id = "";
				$.each(data, function(index, item) {
					if (id != "") {
						id = id + "," + item.id;
					} else {
						id = item.id;
					}
					//item 没有children属性
					if (item.children != null) {
						var i = getCheckedId(item.children);
						if (i != "") {
							id = id + "," + i;
						}
					}
				});
				return id;
			}
			// 提交数据
			form.on("submit(submit)", function(data) {
				if (!data.field.status) {
					data.field.status = "2";
				}
				// 获取选中节点的数据
				var checkedData = tree.getChecked("Treeid");
				permissions = getCheckedId(checkedData);
				$.ajax({
					url: "{:url('cate/edit')}",
					data: {
						id: data.field.id,
						name: data.field.name,
						permissions: permissions,
						status: data.field.status,
						postscript: data.field.postscript
					},
					type: "post",
					success: function(res) {
						if (res.code == 0) {
							top.notify.success(res.msg, "topRight");
							setTimeout(function() {
								var index = parent.layer.getFrameIndex(window
									.name); // 先得到当前 iframe 层的索引
								parent.layer.close(index); // 再执行关闭
							}, 1000)
						} else {
							top.notify.error(res.msg, "topRight");
						}
					}
				})
				return false;
			});
		});
	});
</script>